using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Forecast.Database;

namespace Forecast.Business
{
    public class Medals
    {
        #region Interface

        public int[] GetMedals(DataTable tableMedals)
        {
            if (tableMedals == null)
                return null;
            if (tableMedals.Rows.Count == 0)
                return null;

            int[] medals = new int[tableMedals.Rows.Count];
            int index = 0;
            foreach (DataRow row in tableMedals.Rows)
            {
                medals[index] = (int)row[PredictionResult.ColumnName.Medal_id.ToString()];
                index++;
            }
            return medals;
        }

        public DataTable GetMedalsForPlayer(Player player, IList<Medal> allMedals, IList<PredictionResult> allPredictionResults, IList<Prediction> allClosedPredictions)
        {
            if (player == null)
                throw new ArgumentNullException("player");
            if (allMedals == null)
                throw new ArgumentNullException("allMedals");

            DataTable dataTable = GetEmptyDataTableForMedalsForPlayer();

            foreach (PredictionResult predictionResult in allPredictionResults)
            {
                if (predictionResult.PlayerId != player.Id)
                    continue;
                if (predictionResult.MedalId == 1)
                    continue;

                DataRow row = dataTable.NewRow();
                row["Medal"] = GetMedalName(allMedals, predictionResult.MedalId);
                row[PredictionResult.ColumnName.Medal_id.ToString()] = predictionResult.MedalId;
                row["Ticker"] = GetTicker(allClosedPredictions, predictionResult.PredictionId);
                row[PredictionResult.ColumnName.Prediction_id.ToString()] = predictionResult.PredictionId;
                row["Description"] = GetTickerDescription(allClosedPredictions, predictionResult.PredictionId);
                dataTable.Rows.Add(row);
            }

            return dataTable;
        }

        #endregion
        #region Helper methods

        private DataTable GetEmptyDataTableForMedalsForPlayer()
        {
            DataTable dataTable = new DataTable("MedalsForPlayer");

            DataColumn columnMedal = new DataColumn();
            columnMedal.Caption = "Medal";
            columnMedal.ColumnName = "Medal";
            columnMedal.DataType = typeof(string);
            dataTable.Columns.Add(columnMedal);

            DataColumn columnMedalId = new DataColumn();
            columnMedalId.ColumnName = PredictionResult.ColumnName.Medal_id.ToString();
            columnMedalId.DataType = typeof(int);
            dataTable.Columns.Add(columnMedalId);

            DataColumn columnPrediction = new DataColumn();
            columnPrediction.Caption = "Ticker";
            columnPrediction.ColumnName = "Ticker";
            columnPrediction.DataType = typeof(string);
            dataTable.Columns.Add(columnPrediction);

            DataColumn columnPredictionId = new DataColumn();
            columnPredictionId.ColumnName = PredictionResult.ColumnName.Prediction_id.ToString();
            columnPredictionId.DataType = typeof(int);
            dataTable.Columns.Add(columnPredictionId);

            DataColumn columnPredictionDescription = new DataColumn();
            columnPredictionDescription.Caption = "Description";
            columnPredictionDescription.ColumnName = "Description";
            columnPredictionDescription.DataType = typeof(string);
            dataTable.Columns.Add(columnPredictionDescription);

            return dataTable;
        }

        private string GetMedalName(IList<Medal> allMedals, int medalId)
        {
            foreach (Medal medal in allMedals)
            {
                if (medal.Id == medalId)
                    return medal.Name;
            }
            return "No matching medal found";
        }

        private string GetTicker(IList<Prediction> allClosedPredictions, int predictionId)
        {
            foreach (Prediction prediction in allClosedPredictions)
            {
                if (prediction.Id == predictionId)
                    return prediction.Ticker;
            }
            return "No matching prediction found";
        }

        private string GetTickerDescription(IList<Prediction> allClosedPredictions, int predictionId)
        {
            foreach (Prediction prediction in allClosedPredictions)
            {
                if (prediction.Id == predictionId)
                    return prediction.ShortDescription;
            }
            return "No matching prediction found";
        }

        #endregion
    }
}
